<template>
  <view id="virtualMobile" class="page">
    <view
      class="top-title"
      :style="{ height: customBar + 'px', paddingTop: statusBar + 'px' }"
    >
      <image
        class="img-home"
        @tap="goHome"
        src="../../../image/icon_home.png"
      />
    </view>
    <image :src="imgUrl + 'bg_virtual.png'" class="img" />
    <view class="btn-submit" @tap="onCallPhone">
      <image class="phone" src="../../../image/icon_phone.png" />
      虚拟号呼叫车主</view
    >
    <view class="info">为保护双方隐私，通话将启用虚拟号码</view>
    <view class="info">虚拟号有效期30分钟</view>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import "./virtualMobile.less";
import { setGlobalData, getGlobalData } from "../../../globalData.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
const API_CarCode = process.env.IOParking.API_CARCODE;
export default {
  name: "virtualMobile",
  data() {
    return {
      apiBaseUrl: process.env.IOParking.API_HOST, //请求头部
      imgUrl: process.env.IOParking.IMAGE_CARCODE,
      moveCarCodeNo: "", //挪车码
      virtualMobile: "",
      statusBar: 0, //状态栏高度
      customBar: 0, //胶囊按钮高度
    };
  },
  //mixins:[authorize],
  components: {},
  mounted() {
    Taro.getSystemInfo({
      success: (res) => {
        this.statusBar = res.statusBarHeight; //状态栏高度
        let custom = Taro.getMenuButtonBoundingClientRect(); //菜单按钮
        this.customBar = custom.height + 8;
      },
    });
  },

  onLoad: function (options) {
    console.log("参数:", options.no);
    this.moveCarCodeNo = options.no;
    if (!getGlobalData("openId")) {
      this.wx_login();
    }
    this.getVirtualMobile();
  },
  created() {},
  methods: {
    wx_login() {
      api
        .getUserInfo({
          scope: "all",
        })
        .then((resp) => {
          if (resp.success) {
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
          }
        },
      });
    },
    //回到主页
    goHome() {
      api.openPage("home");
    },
    //获取虚拟号码
    getVirtualMobile() {
      let url =
        API_CarCode +
        "carmovingcode/getvirtualnumber?code=" +
        this.moveCarCodeNo;
      Taro.request({
        url: url,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          console.log("获取虚拟号码：", JSON.stringify(res.data));
          if (res.data.isSuccess) {
            this.virtualMobile = res.data.data;
          } else {
            //
          }
        },
      });
    },
    onCallPhone() {
      if (!this.virtualMobile) {
        Taro.showToast({
          title: "未获取到虚拟号",
          icon: "error",
          duration: 1000,
          mask: true,
        });
        return;
      }
      Taro.makePhoneCall({
        phoneNumber: this.virtualMobile,
      });
    },
    //end
  },
};
</script>

